//
//  SleepListView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/26.
//

#import "SleepListView.h"
#import "SleepSortCell.h"
#import "SleepGroupModel.h"
#import "SleepListVC.h"

@interface SleepListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) ReLayoutButton * healthBtn;
@property (nonatomic,strong) ReLayoutButton * orderBtn;
@property (nonatomic,strong) ReLayoutButton * recordBtn;
@property (nonatomic,strong) ReLayoutButton * boxBtn;
@property (nonatomic,strong) YYLabel * infoLabel;
@property (nonatomic,strong) UITableView * listView;
@property (nonatomic,strong) NSMutableArray * listArray;

@end

@implementation SleepListView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubVuewLayout];
    }
    return self;
}
-(void)setSubVuewLayout{
    
    [self addSubview:self.orderBtn];
    [_orderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_centerX).offset(-10 * FMScaleX);
        make.top.mas_offset(10 * FMScaleX);
        make.size.mas_offset(CGSizeMake(60, 70 * FMScaleX));
    }];
    
    [self addSubview:self.recordBtn];
    [_recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.orderBtn);
        make.size.mas_equalTo(self.orderBtn);
        make.left.equalTo(self.mas_centerX).offset(10 * FMScaleX);
    }];
    
    [self addSubview:self.healthBtn];
    [_healthBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.orderBtn);
        make.size.mas_equalTo(self.orderBtn);
        make.right.equalTo(self.orderBtn.mas_left).offset(-20 * FMScaleX);
    }];
    
    [self addSubview:self.boxBtn];
    [_boxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.orderBtn);
        make.size.mas_equalTo(self.orderBtn);
        make.left.equalTo(self.recordBtn.mas_right).offset(20 * FMScaleX);
    }];
    
    [self addSubview:self.infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(333 * FMScaleX, 90 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.equalTo(self.boxBtn.mas_bottom).offset(20 * FMScaleX);
    }];
    [self addSubview:self.listView];
    [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.infoLabel.mas_bottom).offset(10 * FMScaleX);
        make.bottom.mas_offset(-10 * FMScaleX);
    }];
}

- (void)setInfoModel:(SleepHeadModel *)infoModel{
    
    self.infoLabel.text = [NSString stringWithFormat:@"%@\n%@\n%@",infoModel.depict,infoModel.title,infoModel.chineseDate];
    [self requestData];

}

-(void)requestData{
    [self.listArray removeAllObjects];
    @WeakObj(self)
    [SPNetworkingIns GET: PORT(@"sleep-center/api/sleep/SleepMusicAll") params:@{@"start":@(0)} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        NSArray * dataArr = [responseObject objectForKey:@"data"];
        if (dataArr) {
            for (NSDictionary * item in dataArr) {
                SleepGroupModel * model = [[SleepGroupModel alloc]init];
                model = [SleepGroupModel yy_modelWithDictionary:item];
                [self.listArray addObject:model];
            }
        }
        [self.listView reloadData];
        
        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];
    
}

-(void)healthBtnAction{
    
    SleepListVC * vc = [[SleepListVC alloc]init];
    vc.typeId = 111;
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
}

-(void)orderBtnAction{
    
    SleepListVC * vc = [[SleepListVC alloc]init];
    vc.typeId = 222;
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
}

-(void)recordBtnAction{
    
    SleepListVC * vc = [[SleepListVC alloc]init];
    vc.typeId = 333;
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
    
}

-(void)boxBtnAction{
    
    SleepListVC * vc = [[SleepListVC alloc]init];
    vc.typeId = 444;
    [[UIViewController findNavigationController] pushViewController:vc animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 200 * FMScaleX;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SleepSortCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SleepSortCell"];
    SleepGroupModel * model = self.listArray[indexPath.row];
    if (model) {
        [cell setInfoModel:model];
    }
    return cell;
}


-(ReLayoutButton *)healthBtn{
    
    if (!_healthBtn) {
        _healthBtn = [[ReLayoutButton alloc]init];
        [_healthBtn setTitle:@"自然声" forState:UIControlStateNormal];
        [_healthBtn setImage:[UIImage imageNamed:@"Sleep_list_zrs"] forState:UIControlStateNormal];

        _healthBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _healthBtn.layoutType = RelayoutTypeUpDown;
        _healthBtn.margin = 9;
        [_healthBtn setTitleColor:RDRGB(0xffffff) forState:UIControlStateNormal];
        [_healthBtn addTarget:self action:@selector(healthBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _healthBtn.titleLabel.font = Regular(14);
    }
    return _healthBtn;
}

-(ReLayoutButton *)orderBtn{
    
    if (!_orderBtn) {
        _orderBtn = [[ReLayoutButton alloc]init];
        [_orderBtn setTitle:@"冥想" forState:UIControlStateNormal];
        [_orderBtn setImage:[UIImage imageNamed:@"Sleep_list_mx"] forState:UIControlStateNormal];

        _orderBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _orderBtn.layoutType = RelayoutTypeUpDown;
        _orderBtn.margin = 9;
        [_orderBtn setTitleColor:RDRGB(0xffffff) forState:UIControlStateNormal];
        [_orderBtn addTarget:self action:@selector(orderBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _orderBtn.titleLabel.font = Regular(14);
    }
    return _orderBtn;
}

-(ReLayoutButton *)recordBtn{
    
    if (!_recordBtn) {
        _recordBtn = [[ReLayoutButton alloc]init];
        [_recordBtn setTitle:@"助眠" forState:UIControlStateNormal];
        [_recordBtn setImage:[UIImage imageNamed:@"Sleep_list_zm"] forState:UIControlStateNormal];

        _recordBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _recordBtn.layoutType = RelayoutTypeUpDown;
        _recordBtn.margin = 9;
        [_recordBtn setTitleColor:RDRGB(0xffffff) forState:UIControlStateNormal];
        [_recordBtn addTarget:self action:@selector(recordBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _recordBtn.titleLabel.font = Regular(14);
    }
    return _recordBtn;
}

-(ReLayoutButton *)boxBtn{
    
    if (!_boxBtn) {
        _boxBtn = [[ReLayoutButton alloc]init];
        [_boxBtn setTitle:@"轻音乐" forState:UIControlStateNormal];
        [_boxBtn setImage:[UIImage imageNamed:@"Sleep_list_qyy"] forState:UIControlStateNormal];

        _boxBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _boxBtn.layoutType = RelayoutTypeUpDown;
        _boxBtn.margin = 9;
        [_boxBtn setTitleColor:RDRGB(0xffffff) forState:UIControlStateNormal];
        [_boxBtn addTarget:self action:@selector(boxBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _boxBtn.titleLabel.font = Regular(14);
    }
    return _boxBtn;
}
-(YYLabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[YYLabel alloc]init];
        _infoLabel.backgroundColor = RDRGBA(0xF8F9F6, 0.4);
        [_infoLabel setCornerRadius:9 * FMScaleX borderWidth:1 borderColor:RDRGBA(0xF8F9F6, 0.3)];
        _infoLabel.textColor = UIColor.whiteColor;
        _infoLabel.font = Regular(14);
        _infoLabel.numberOfLines = 0;
        _infoLabel.textContainerInset = UIEdgeInsetsMake(5 * FMScaleX, 10 * FMScaleX, 5 * FMScaleX, 10 * FMScaleX);
    }
    return _infoLabel;
}

-(UITableView *)listView{
    
    if (!_listView) {
        _listView = [[UITableView alloc]init];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listView.backgroundColor = UIColor.clearColor;
        [_listView registerClass:[SleepSortCell class] forCellReuseIdentifier:@"SleepSortCell"];
    }
    return _listView;
}
-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}
@end
